package com.xjscrm.console.service.customer.chat;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.console.dto.ChatSettingsDTO;
import com.xjscrm.console.entity.ChatSettings;
import com.xjscrm.console.mapper.ChatSettingsMapper;
import com.xjscrm.console.vo.chat.ChatSettingsVO;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/22 17:29
 */
@Service
public class ChatSettingsService {

    @Resource
    private ChatSettingsMapper chatSettingsMapper;


    public int deleteByPrimaryKey(Long id) {
        return chatSettingsMapper.deleteByPrimaryKey(id);
    }


    public int insertSelective(ChatSettings record) {
        return chatSettingsMapper.insertSelective(record);
    }


    public ChatSettings selectByPrimaryKey(Long id) {
        return chatSettingsMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(ChatSettings record) {
        return chatSettingsMapper.updateByPrimaryKeySelective(record);
    }

    public ChatSettings selectByMerId(@MerId Long merId) {
        return chatSettingsMapper.selectByMerId(merId);
    }

    public Object openOrCloseKeywordShield(@MerId Long merId, Integer openKeywordShield) {
        ChatSettings chatSettings = chatSettingsMapper.selectByMerId(merId);

        ChatSettings update = new ChatSettings();
        update.setId(chatSettings.getId());
        update.setOpenKeywordShield(openKeywordShield);
        update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        chatSettingsMapper.updateByPrimaryKeySelective(update);

        return true;
    }

    public Object saveKeywordShield(@MerId Long merId, ChatSettingsDTO chatWwDeviceOpt) {

        ChatSettings chatSettings = chatSettingsMapper.selectByMerId(merId);

        ChatSettings update = new ChatSettings();
        update.setId(chatSettings.getId());
        if (Objects.nonNull(chatWwDeviceOpt.getOpenKeywordShield())) {
            update.setOpenKeywordShield(chatWwDeviceOpt.getOpenKeywordShield());
        }
        if (ObjectUtils.isNotEmpty(chatWwDeviceOpt.getExactKeyword())) {
            update.setExactKeyword(JSON.toJSONString(chatWwDeviceOpt.getExactKeyword()));
        } else {
            update.setExactKeyword("[]");
        }
        if (ObjectUtils.isNotEmpty(chatWwDeviceOpt.getBlurKeyword())) {
            update.setBlurKeyword(JSON.toJSONString(chatWwDeviceOpt.getBlurKeyword()));
        } else {
            update.setBlurKeyword("[]");
        }
        update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        chatSettingsMapper.updateByPrimaryKeySelective(update);
        return true;
    }

    public ChatSettingsVO getKeywordShield(@MerId Long merId) {
        ChatSettings chatSettings = chatSettingsMapper.selectByMerId(merId);
        if (Objects.isNull(chatSettings)) {
            chatSettings = this.init(merId);
        }
        return convert(chatSettings);
    }

    private ChatSettings init(@MerId Long merId) {

        ChatSettings insert = new ChatSettings();
        insert.setId(IdUtil.getSnowflakeNextId());
        insert.setMerId(merId);
        insert.setOpenKeywordShield(0);
        insert.setExactKeyword("[]");
        insert.setBlurKeyword("[]");
        insert.setCreateTime((int) (System.currentTimeMillis() / 1000L));
        insert.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        chatSettingsMapper.insertSelective(insert);

        return insert;
    }

    private ChatSettingsVO convert(ChatSettings chatSettings) {
        ChatSettingsVO chatSettingsVO = new ChatSettingsVO();

        if (ObjectUtils.isNotEmpty(chatSettings.getExactKeyword())) {
            chatSettingsVO.setExactKeyword(JSON.parseArray(chatSettings.getExactKeyword(), String.class));
        } else {
            chatSettingsVO.setExactKeyword(Lists.newArrayList());
        }
        if (ObjectUtils.isNotEmpty(chatSettings.getBlurKeyword())) {
            chatSettingsVO.setBlurKeyword(JSON.parseArray(chatSettings.getBlurKeyword(), String.class));
        } else {
            chatSettingsVO.setBlurKeyword(Lists.newArrayList());
        }
        chatSettingsVO.setOpenKeywordShield(chatSettings.getOpenKeywordShield());
        chatSettingsVO.setMerId(chatSettings.getMerId());
        chatSettingsVO.setId(chatSettings.getId());

        return chatSettingsVO;
    }
}
